/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Model.DAO;
import Model.Pojo.Agents;
import java.util.List;
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import org.hibernate.Query;
import org.hibernate.Session;
/**
 *
 * @author luong
 */
public class  AgentImp implements AgentDAO {

    Agents agent;
    
    @Override
    public Agents Entrer(String userNameAgents, String passAgents) {
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();
        String hql = String.format("select a from Agents a where a.userNameAgents='%s' and a.passAgents='%s'", userNameAgents, passAgents);
        try {
            agent = (Agents) session.createQuery(hql).uniqueResult();
        } catch (Exception e) {
            System.err.print(e.getMessage());
        }
        finally
        {
            session.close();
        }
        return agent;     
    }

    @Override
    public boolean AjouterUser(Agents agent) {
        boolean Resultat=false;
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();
        try {            
            session.save(agent);
            session.beginTransaction().commit();
            Resultat=true;
        } catch (Exception e) {
            System.err.print(e.getMessage());
        }
        finally
        {
            session.close();
        }
        return Resultat;
    }

    @Override
    public boolean SuprimerUser(Agents agent) {
        boolean Resultat=false;
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();
        try {           
            String hql= String.format("select a from Agents a where a.userNameAgents='%s'",agent.getUserNameAgents());
            agent= (Agents) session.createQuery(hql).uniqueResult();
            session.delete(agent);
            session.beginTransaction().commit();
//            String hql="delete from Agents where UserNameAgents = :UserNameAgent";
//            Query query=session.createQuery(hql);
//            query.setString("UserNameAgent", "vaduc");
//            query.executeUpdate();
            Resultat=true;
        } catch (Exception e) {
            System.err.print(e.getMessage());
        }
        finally
        {
            session.close();
        }
        return Resultat;
    }

    @Override
    public boolean UpdateUser(Agents agent) {
        boolean Resultat=false;
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();
        try {                       
            session.update(agent);
            session.beginTransaction().commit();
            Resultat=true;
        } catch (Exception e) {
            System.err.print(e.getMessage());
        }
        finally
        {
            session.close();
        }
        return Resultat;
    }

    @Override
    public List<Agents> GetListAgents() {
        List<Agents> listAgent = null; 
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();
        try {
            String hql="from Agents";
            Query query= session.createQuery(hql);
            listAgent=query.list(); 
        } catch (Exception e) {
            System.err.print(e.getMessage());
        }
        finally
        {
            session.close();
        }
        return listAgent;
    }

    @Override
    public Agents CheckAgents(String userNameAgents) {
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();
        String hql = String.format("select a from Agents a where a.userNameAgents='%s'", userNameAgents);
        try {
            agent = (Agents) session.createQuery(hql).uniqueResult();
        } catch (Exception e) {
            System.err.print(e.getMessage());
        }
        finally
        {
            session.close();
        }
        return agent; 
    }
    
    
}
